
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { GameConfig } from "../config/GameConfig";
import { RoomMatchConfig } from "../config/RoomMatchConfig";
import { ViewConfig } from "../config/ViewConfig";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewMatchGame extends ViewBase {

    private gameTypeConfig: Array<number> = [
        GameConfig.Game_Type.ddz,
        GameConfig.Game_Type.jianziding,
        GameConfig.Game_Type.dazhonger_mj
    ]

    onEnable(): void {
        super.onEnable()
        let btnBack: cc.Node = this.findChild(this.node, "btnBack")
        UIHelper.onAddEvent(btnBack, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewHall, () => {
                this.closeView()
            })
        })
        SocketClient.req_match_roomconfig(() => {
            this.refresh()
        })
        this.refresh()
    }

    private refresh() {
        for (let i = 0; i < 3; i++) {
            let game: cc.Node = this.findChild(this.node, "scrollView/view/content/game" + i)
            let config: RoomMatchConfig = RoomMatchConfig.getRoomMatchConfig(this.gameTypeConfig[i])
            let lbCost: cc.Label = this.getChild(game, "lbCost", cc.Label)
            let lbOnline: cc.Label = this.getChild(game, "lbOnline", cc.Label)
            if(config){
                lbCost.string = config.serve + ""
                lbOnline.string = config.online + ""
            }
            UIHelper.onAddEvent(game, () => {
                if (i == 0) {
                    SocketClient.req_match_enroll(GameConfig.Game_Type.ddz, () => {

                    })
                }
                else if (i == 1) {
                    SocketClient.req_match_enroll(GameConfig.Game_Type.jianziding, () => {

                    })
                }
                else {
                    SocketClient.req_match_enroll(GameConfig.Game_Type.dazhonger_mj, () => {

                    })
                }
            })
        }
    }
}
